﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;

namespace Galaxy.AutoCAD.Tests
{
    public class OptimizeClass
    {
        [CommandMethod("OptCommand")]
        public void OptCommand()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            string fileName = @"D:\autocad.net\Galaxy\HelloWorld\bin\Release\HelloWorld.dll";
            ExtensionLoader.Load(fileName);
            ed.WriteMessage("\n" + fileName + "被载入，请输入Hello进行测试！");
        }
        [CommandMethod("ChangeColor")]
        public void ChangeColor()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            try
            {
                ObjectId id = ed.GetEntity("\n请选择要改变颜色的对象").ObjectId;
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    Entity ent = (Entity)trans.GetObject(id, OpenMode.ForWrite);
                    ent.ColorIndex = 300;
                    trans.Commit();
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                switch (ex.ErrorStatus)
                {
                    case ErrorStatus.InvalidIndex:
                        ed.WriteMessage("\n输入的颜色值有误！");
                        break;
                    case ErrorStatus.InvalidObjectId:
                        ed.WriteMessage("\n未选择对象！");
                        break;
                    default:
                        ed.WriteMessage(ex.ErrorStatus.ToString());
                        break;
                }
            }
        }
    }
}
